let { projectRootPath } = require("../../util/path/index.js");
let {
  executeSql,
  transaction,
  getTotal,
  getByPagination,
  removeByUuid,
  getAll,
} = require(`${projectRootPath}/mysql/index.js`);
let { getUuid } = require(`${projectRootPath}/util/uuid/index.js`);
let { getCurrentDatetime } = require(
  `${projectRootPath}/util/datetime/index.js`,
);

// 创建任务
async function createTask(data) {
  let { userUuid, content } = data;
  return await transaction(async (connection) => {
    let currentDatetime = getCurrentDatetime();
    let sql = `insert into TaskTable (uuid, userUuid, content, sendDatetime, isComplete, opDatetime) values ('${getUuid()}', '${userUuid}', '${content}', '${currentDatetime}', '2', '${currentDatetime}')`;
    await executeSql(connection, sql);
  });
}
// 获取任务分页数据
async function getTaskByPagination(data) {
  let { pageSize, currentPage, userUuid, sendDatetimeRange, isComplete } = data;
  let where = "";
  let allWhere = [];
  if (userUuid) {
    where = `userUuid='${userUuid}'`;
    allWhere.push(where);
  }
  if (sendDatetimeRange.length === 1) {
    where = `sendDatetime between '${sendDatetimeRange[0]} 00:00:00' and '${sendDatetimeRange[0]} 23:59:59'`;
    allWhere.push(where);
  } else if (sendDatetimeRange.length === 2) {
    where = `sendDatetime between '${sendDatetimeRange[0]} 00:00:00' and '${sendDatetimeRange[1]} 23:59:59'`;
    allWhere.push(where);
  }
  if (isComplete !== "") {
    where = `isComplete='${isComplete}'`;
    allWhere.push(where);
  }
  if (allWhere.length > 0) {
    allWhere = "where " + allWhere.join(" and ");
  } else {
    allWhere = "";
  }
  return await transaction(async (connection) => {
    let list = await getByPagination(
      connection,
      "TaskTable",
      "opDatetime",
      allWhere,
      pageSize,
      currentPage,
    );
    let total = await getTotal(connection, "TaskTable", "opDatetime", allWhere);
    return { list, total };
  });
}
// 通过 uuid 编辑任务
async function editTaskByUuid(data) {
  let { uuid, userUuid, content, isComplete } = data;
  return await transaction(async (connection) => {
    let sql = `update TaskTable set userUuid='${userUuid}', content='${content}', isComplete='${isComplete}' where uuid='${uuid}'`;
    await executeSql(connection, sql);
  });
}
// 通过 uuid 删除任务
async function removeTaskByUuid(data) {
  let { uuid } = data;
  return await transaction(async (connection) => {
    await removeByUuid(connection, "TaskTable", uuid);
  });
}
// 获取全部任务
async function getTaskByAll() {
  return await transaction(async (connection) => {
    let list = await getAll(connection, "TaskTable", "opDatetime");
    return { list };
  });
}

// 获取未完成数量
async function getTaskNotCompleteCount(data) {
  let { userUuid } = data;
  return await transaction(async (connection) => {
    let sql = `select count(*) as count from TaskTable  where userUuid='${userUuid}' and isComplete='2' order by opDatetime`;
    let result = await executeSql(connection, sql);
    return { count: result[0].count };
  });
}

module.exports = {
  createTask,
  getTaskByPagination,
  editTaskByUuid,
  removeTaskByUuid,
  getTaskByAll,
  getTaskNotCompleteCount,
};
